<?php
include_once '../../lib/config.php';
include_once '../../lib/db.php';
include_once '../../lib/utils/tag.php';
include_once '../../model/detil_prestasipelanggaran.php';
include_once '../../model/prestasipelanggaran.php';

$proc = $_REQUEST['proc'];

switch ($proc){
	case "deprepel-table":
		$key = $_POST['key'];
		$key2 = $_POST['key2'];
		$orderby = $_POST['orderby'];
		include '../../view/admin/detil_prestasipelanggaran/tableList.php';
		break;
		
	case "add-modal":
		include '../../view/admin/detil_prestasipelanggaran/deprepel_add.php';
		break;
		
	case "deprepel-add":
		$DETACHIEV_ID = $_POST['deprepel_id'];
		$DETACHIEV_NAMA = $_POST['deprepel_nama'];
		$DETACHIEV_TINGKAT = $_POST['deprepel_tingkat'];
		$prepel_jenis = $_POST['prepel_jenis'];
		$ACHIEV_ID = $_POST['prepel_id'][$prepel_jenis];
		if (!validate_input($DETACHIEV_ID, $DETACHIEV_NAMA, $DETACHIEV_TINGKAT, $ACHIEV_ID)) return;
		$ex = deprepel_add($DETACHIEV_ID, $DETACHIEV_NAMA, $DETACHIEV_TINGKAT, $ACHIEV_ID);
		echo json_encode(array( 'error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno()) ));
		break;
		
	case "edit-modal":
		$deprepel_id = $_POST['deprepel_id'];
		include '../../view/admin/detil_prestasipelanggaran/deprepel_edit.php';
		break;
		
	case "deprepel-edit":
		$DETACHIEV_ID = $_POST['deprepel_id'];
		$DETACHIEV_NAMA = $_POST['deprepel_nama'];
		$DETACHIEV_TINGKAT = $_POST['deprepel_tingkat'];
		$prepel_jenis = $_POST['prepel_jenis'];
		$ACHIEV_ID = $_POST['prepel_id'][$prepel_jenis];
		if (!validate_input($DETACHIEV_ID, $DETACHIEV_NAMA, $DETACHIEV_TINGKAT, $ACHIEV_ID)) return;
		$ex = deprepel_update($DETACHIEV_ID, $DETACHIEV_NAMA, $DETACHIEV_TINGKAT, $ACHIEV_ID);
		echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));
		break;
		
	case "deprepel-delete":
		$DETACHIEV_ID = $_POST['deprepel_id'];
		$ex = deprepel_delete($DETACHIEV_ID);
		echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));
}

function validate_input($DETACHIEV_ID, $DETACHIEV_NAMA, $DETACHIEV_TINGKAT, $ACHIEV_ID){
	if ($DETACHIEV_ID === '' || is_null($DETACHIEV_ID)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi ID Detil Prestasi/Pelanggaran")); return ;
	}
	if ($DETACHIEV_NAMA === '' || is_null($DETACHIEV_NAMA)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi NAMA Detil Prestasi/Pelanggaran")); return ;
	}
	if ($DETACHIEV_TINGKAT === '' || is_null($DETACHIEV_TINGKAT)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi TINGKAT Detil Prestasi/Pelanggaran")); return ;
	}
	return true;
}